﻿using System;
using System.ServiceProcess;
using System.ServiceModel;
using CommunicationDataContract;
using System.Diagnostics;

namespace CommunicationDesktopApplication
{
    public class WindowsServiceApplicationConnection : IDisposable
    {
        #region Private Fields
        private Guid  DesktopApplicationId;
        private IWindowsServiceCommunicationEndPointContract ServiceCommunicationEndPoint;
        private ServiceController WindowsServiceController = new ServiceController(EndpointSettings.WindowsServiceName);
        NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
        EndpointAddress endpoint = new EndpointAddress(EndpointSettings.WindowsServiceEndpointAddress);
        #endregion

        #region Constructors
        public WindowsServiceApplicationConnection(DesktopServiceController desktopController)
        {
            DesktopApplicationId = desktopController.ApplicationId;
        }
        #endregion

        #region Methods
        /// <summary>
        /// List Attached Applications Guids
        /// </summary>
        /// <returns></returns>
        public Guid[] GetAttachedApplicationIds()
        {
            ServiceCommunicationEndPoint = ChannelFactory<IWindowsServiceCommunicationEndPointContract>.CreateChannel(binding, endpoint);
            return ServiceCommunicationEndPoint.AttachedApplications(DesktopApplicationId);
        }

        /// <summary>
        /// Stop Process the Running Windows Service EndPoint
        /// </summary>
        public void JobPause()
        {

            ServiceCommunicationEndPoint = ChannelFactory<IWindowsServiceCommunicationEndPointContract>.CreateChannel(binding, endpoint);
            ServiceCommunicationEndPoint.JobPause(DesktopApplicationId);
        }
        /// <summary>
        /// Start Process the Running Windows Service EndPoint
        /// </summary>
        public void JobStart()
        {

            ServiceCommunicationEndPoint = ChannelFactory<IWindowsServiceCommunicationEndPointContract>.CreateChannel(binding, endpoint);
            ServiceCommunicationEndPoint.JobStart(DesktopApplicationId);
        }
        /// <summary>
        /// Start Windows Service
        /// </summary>
        public void WindowsServiceStart()
        {
            WindowsServiceController.Start();
        }
        /// <summary>
        /// Stop Windows Service
        /// </summary>
        public void WindowsServiceStop()
        {
            WindowsServiceController.Stop();
        }
        /// <summary>
        /// Determine if Windows Service Process is Running
        /// </summary>
        /// <returns>Indicates if Windows Service is Running</returns>
        public Boolean IsJobRunning()
        {

            ServiceCommunicationEndPoint = ChannelFactory<IWindowsServiceCommunicationEndPointContract>.CreateChannel(binding, endpoint);
            return IsWindowsServiceRunning() && !ServiceCommunicationEndPoint.IsJobPaused(DesktopApplicationId);
        }
        /// <summary>
        /// Determine if Windows Service is Running
        /// </summary>
        /// <returns>Indicates if Windows Service is Running</returns>
        public Boolean IsWindowsServiceRunning()
        {
            return WindowsServiceController != null && WindowsServiceController.Status == ServiceControllerStatus.Running;
        }
        #endregion

        #region IDisposable
        public void Dispose()
        {
            ServiceCommunicationEndPoint = null;
        }
        #endregion
    }
}
